<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------


// 应用公共文件
/**
 * 创建一个32位随机字符串
 * @param number $length
 * @return string
 */
function createNoncestr( $length = 32 ){
    $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
    $str ="";
    for ( $i = 0; $i < $length; $i++ )  {
        $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
    }
    return $str;
}
/**
 * 计算2个时间戳的相差的天数
 * @param string $beginTime 开始时间戳
 * @param string $endTime 结束时间戳
 * @param string $type 类型 默认为 second 秒时间戳，msec 位毫秒时间戳
 * @return array $result
 * @todo  优化时间显示
 */
function  evaluateTimeDiffer($beginTime,$endTime,$type){
    if(!empty($type) || $type == 'msec'){
        $beginTime = $beginTime/1000;
        $endTime = $endTime/1000;
    }
    if($beginTime - $endTime > 0){
        $start = $endTime;
        $end = $beginTime;
    }
    else{
        $start = $beginTime;
        $end = $endTime;
    }
    if(($end-$start) != 0){
        $result['days'] = intval(($end-$start)/86400);
        $remain = ($end-$start)%86400;
    }
    if($remain != 0){
        $result['hours'] = intval($remain/3600);
        $remain = $remain%3600;
    }
    if($remain != 0){
        $result['minutes'] = intval($remain/60);
        $result['seconds'] = $remain%60;
    }
    if(!empty($result['days'])){
        $result['chString'] .= $result['days'].'天';
    }
    if(!empty($result['hours'])){
        $result['chString'] .= $result['hours'].'小时';
    }
    if(!empty($result['minutes'])){
        $result['chString'] .= $result['minutes'].'分钟';
    }
    if(!empty($result['seconds'])){
        $result['chString'] .= $result['seconds'].'秒';
    }
    return $result;
}
/**
 * 得到客户端ip地址
 */
function getOnlineIp() {
    if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
        $online_ip = getenv('HTTP_CLIENT_IP');
    } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
        $online_ip = getenv('HTTP_X_FORWARDED_FOR');
    } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
        $online_ip = getenv('REMOTE_ADDR');
    } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
        $online_ip = $_SERVER['REMOTE_ADDR'];
    }

    preg_match("/[\d\.]{7,15}/", $online_ip, $online_ipmatches);
    $online_ip = $online_ipmatches[0] ? $online_ipmatches[0] : 'unknown';
    return $online_ip;
}
/**
 * 得到本地ip地址
 */
function getServerIp()
{
    if ($_SERVER['REMOTE_ADDR']) {
        $sip = $_SERVER['REMOTE_ADDR'];
    } elseif (getenv("REMOTE_ADDR")) {
        $sip = getenv("REMOTE_ADDR");
    } elseif (getenv("HTTP_CLIENT_IP")) {
        $sip = getenv("HTTP_CLIENT_IP");
    } else {
        $sip = "unknown";
    }
    return $sip;
}
/**
 * 记录日志
 * @param array or string $word
 */
function  logFile($word){
    $path = 'data/logs/log/';
    $ip = getOnlineIp();
    $localIp = getServerIp();
    if(!is_dir($path)){
        mkdir($path);
        chmod($path, 0777);
    }
    $file = $path.date('Ymd').'.log';
    $fp = fopen($file,"a");
    flock($fp, LOCK_EX) ;
    fwrite($fp,"time：".strftime("%Y-%m-%d %H:%M:%S",time())." localIp:".$localIp." ip:".$ip." operation:".arrayTostr($word,true)."\n\n");
    flock($fp, LOCK_UN);
    fclose($fp);
}
/**
 * 把多维数组转化为字符串
 * @param array or string $arr
 * @return string
 */
function arrayTostr($arr){
    if(is_array($arr) || is_object($arr)){
        $flag = is_array($arr) && ( empty($arr) || array_keys($arr) === range(0,count($arr)-1) );
        if($flag){
            $str = '[' . implode(',', array_map('arrayTostr', $arr) ) . ']';
        }
        else{
            $items = Array();
            foreach( $arr as $key => $value ) {
                $items[] = arrayTostr($key) . ':' . arrayTostr($value);
            }
            $str = '{' . implode(',', $items) . '}';
        }
    }
    elseif(is_string($arr)){
        $str = $arr;
    }
    else {
        $str = strtolower(var_export( $arr, true ));
    }
    return $str;
}

/*
 *
 * 根据对象转换为数组
 * @param object
 * @return Array
 * */
function object2array($object)
{
    $array = Array();
    if (is_object($object)) {
        foreach ($object as $key => $value) {
            $array[$key] = $value;
        }
    } else {
        $array = $object;
    }
    return $array;
}
/*
 *
 * 判断数组数字是否连续
 * @param $array
 * @return Array
 * */
function checkNumberContinuous($data) {
    //一条数据的情况不做处理
    if(count($data) == 1){
        return array();
    }
    $i = 0;$j = 0;
    $result = array();
    $length = count($data)-1;
    foreach ($data as $key=>$value){
        if($key<$length){
            if(($data[$i]+1) == $data[$i+1]){
                $result[$j][] = $data[$i];
            }else{
                $result[$j][] = $data[$i];
                $j++;   
            }
            $i++;
        }else{
            if(($data[$i]-1)== $data[$i-1]){
                $result[$j][] = $data[$i];
            }else{
                $result[$j+1][] = $data[$i];           
            }
        }
    }
    return $result;
}
/*
 *
 * 处理json格式
 * @param array $info
 * @return json
 * */
function urlencodeCustomAction($info){
    if(empty($info)){
        return false;
    }
    if(is_array($info)){
        foreach($info as $k=>$v){
            if(is_array($v)){
                $data[$k] = urlencodeCustomAction($v);
            }else{
                if(gettype($v) == "string"){
                    $data[$k] = urlencode($v);
                }else{
                    $data[$k] = $v;
                }
            }
        }
    }else{
        if(gettype($info) == "string"){
           $data = urlencode($info);
        }else{
            $data = $info;
        }
    }
    return $data;
}
/*
 *
 * 打印方法
 * @param object
 * @return Array
 * */
function P($param){
    echo '<pre>';
    if(is_array($param)){
       print_r($param); 
    }else{
        var_dump($param);
    }
}
